<?php //tailvh@gmail.com

class Linking
{
	function createSession($id, $modelsBranch, $roleBranch, &$session)
	{
		//use id set current branch
		if(isset($id))
		{
			$branchId = $id;
			$branchNode = $modelsBranch->_find($id);
		}
		else
		{
			//if id null get role branch
			$branchNode = $roleBranch;
			$branchId = $branchNode->id;
			$roleUserLevel = $branchNode->level;
		}
		
		//retrieve currentBranch values
		$branchLevel = $branchNode->level;
		$branchName = $branchNode->name;
		
		//parent's link
		if(isset($session))
		{
			$links = $session;
		}
		else
		{
			//get parent trace with array type
			$arrayTrace = explode('-', $branchNode->trace);
			//loop from role branch to current branch => fill SESSION with trace info
			for($i = $roleBranch->level; $i<$branchLevel; $i++)
			{
				//init value for session
				$tempId = (int)$arrayTrace[$i-1];
				$elementLinkInfo = $modelsBranch->_find($tempId);
				$links[$i] = array('name'=>$elementLinkInfo->name, 'id'=>$elementLinkInfo->id);
				$session = $links;
			}
		}
		
		$links[$branchLevel] = array('name'=>$branchName, 'id'=>$branchId);
        $session = $links;
        
        //return 
        return $branchNode;
	}
}
  